<template>
  <div class style="padding:10px">
    <el-form
      size="mini"
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <my-form-item label="用户名" >
        <el-input
          type="username"
          v-model="username"
          autocomplete="off"
        
          disabled="disabled"
        ></el-input>
      </my-form-item>
      <my-form-item label="旧密码" prop="oldPassword" >
        <el-input
          type="password"
          v-model="ruleForm.oldPassword"
        
          autocomplete="off"
        ></el-input>
      </my-form-item>
      <el-row>
        <el-col :span="ruleForm.password?16:24">
          <my-form-item label="新密码" prop="password" >
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </my-form-item>
        </el-col>
        <el-col :span="8" style="margin:0 auto;" v-show="ruleForm.password">
          <div class>
            <div class="input_span">
              <span id="one"></span>
              <span id="two"></span>
              <span id="three"></span>
            </div>
            <div id="font">
              <span>弱</span>
              <span>中</span>
              <span>强</span>
            </div>
          </div>
        </el-col>
      </el-row>

      <my-form-item label="确认新密码" prop="checkPass" >
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </my-form-item>
      <my-form-item style="text-align:right;">
       <el-button size="mini" type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button  size="mini" @click="resetForm('ruleForm')">重置</el-button>
      </my-form-item>
    </el-form>
  </div>
</template>
<script>
const checkStrong = function(sValue) {
  var modes = 0;
  //正则表达式验证符合要求的
  if (sValue.length < 1) return modes;
  if (/\d/.test(sValue)) modes++; //数字
  if (/[a-z]/.test(sValue)) modes++; //小写
  if (/[A-Z]/.test(sValue)) modes++; //大写
  if (/\W/.test(sValue)) modes++; //特殊字符

  //逻辑处理
  switch (modes) {
    case 1:
      return 1;
      break;
    case 2:
      return 2;
      break;
    case 3:
    case 4:
      return sValue.length < 10 ? 3 : 4;
      break;
  }
  return modes;
};
import { changePass } from "./function";
export default {
  data() {
    let validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("password");
        }
        callback();
      }
    };
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value.indexOf(" ") > -1) {
        callback(new Error("密码不能含有空格！"));
      } else if (value.length < 6) {
        callback(new Error("密码长度不能低于6"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      username: JSON.parse(sessionStorage.getItem("user")).username,
      ruleForm: {
        oldPassword: "",
        password: "",
        checkPass: ""
      },
      rules: {
        oldPassword: [
          { required: true, validator: validateOldPass, trigger: "blur" }
        ],
        password: [
          { required: true, validator: validatePass, trigger: "blur" }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    "ruleForm.password": (newValue, oldValue) => {
      this.msgText = checkStrong(newValue);
      if (this.msgText > 1 || this.msgText == 1) {
        document.querySelector("#one").style.background = "red";
      } else {
        document.querySelector("#one").style.background = "#ccc";
      }
      if (this.msgText > 2 || this.msgText == 2) {
        document.querySelector("#two").style.background = "orange";
      } else {
        document.querySelector("#two").style.background = "#ccc";
      }
      if (this.msgText == 4) {
        document.querySelector("#three").style.background = "#00D1B2";
      } else {
        document.querySelector("#three").style.background = "#ccc";
      }
    }
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        let user = JSON.parse(sessionStorage.getItem("user"));
        let params = {
          oldPassword: this.ruleForm.oldPassword,
          password: this.ruleForm.password,
          checkPass: this.ruleForm.checkPass,
          username: user.username
        };
        console.log(params);
        if (valid) {
          changePass(params).then(res => {
            console.log(res);
            if (res.retHead.code === "0000000") {
              this.ruleForm = {};
              this.$message({
                message: "修改成功",
                type: "success"
              });
            } else {
              this.$message.error("修改失败：" + res.retHead.message);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="scss" scoped>
.input_span span {
  display: inline-block;
  width: 85px;
  height: 10px;
  background: #ccc;
  margin-top: 8px;
}

.input_span,
.font {
  display: table-cell;
}
.font span {
  text-align: center;
}
#one {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-right: 0px solid;
  margin-left: 20px;
  margin-right: 3px;
}

#two {
  border-left: 0px solid;
  border-right: 0px solid;
  margin-left: -5px;
  margin-right: 3px;
}

#three {
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  border-left: 0px solid;
  margin-left: -5px;
}
#font span:nth-child(1) {
  color: red;
  margin-left: 60px;
}
#font span:nth-child(2) {
  color: orange;
  margin: 0 60px;
}
#font span:nth-child(3) {
  color: #00d1b2;
}
</style>
